<?php

class UserModel {
    
    /**
     * @var \PDO
     */
    private $db;
    
    function __construct($db) {
        $this->db = $db;
    }

    public function all($get){
        $order_by = $this->buildOrder($get);
        $where = $this->buildWhere($get);
        $sql = "SELECT 
                    users.id, 
                    users.gid, 
                    groups.name AS group_name, 
                    users.pib, 
                    users.email, 
                    users.phone, 
                    users.pass 
                FROM 
                    users 
                    LEFT JOIN groups ON users.gid = groups.id 
                $where
                $order_by";
        $prepare = $this->db->prepare($sql);
        $prepare->execute();
        return $prepare->fetchAll();
    }
    
    private function buildWhere($get){
        if ( array_key_exists('fc', $get) ){
            return " WHERE ({$get['fc']} LIKE '%{$get['fv']}%') ";
        }
        return '';
    }
    
    private function buildOrder($get){
        $order_by = '';
        if (array_key_exists('sc', $get) ){
            $scol = $get['sc'];
            $sord = $get['so'];
            switch ( $scol ) {
                case 'id':
                    $order_by .= " users.id ";
                    break;
                default:
                    $order_by .= " $scol ";
                    break;
            }
            $order_by = " ORDER BY ".$order_by.$sord;
        }
        return $order_by;
    }
    
    public function groups(){
        $prepare = $this->db->prepare('SELECT id, name FROM groups');
        $prepare->execute();
        return $prepare->fetchAll();
    }
    
    public function add($gid, $pib, $email, $phone, $pass){
        $prepare = $this->db->prepare('INSERT INTO users (id, gid, pib, email, phone, pass) VALUES(null, :gid, :pib, :email, :phone, :pass)');
        $prepare->bindParam('gid', $gid, PDO::PARAM_INT);
        $prepare->bindParam('pib', $pib, PDO::PARAM_STR);
        $prepare->bindParam('email', $email, PDO::PARAM_STR);
        $prepare->bindParam('phone', $phone, PDO::PARAM_STR);
        $prepare->bindParam('pass', $pass, PDO::PARAM_STR);
        $prepare->execute();
    }
    
    public function edit($id, $gid, $pib, $email, $phone, $pass){
        $prepare = $this->db->prepare('UPDATE users SET gid = :gid, pib = :pib, email = :email, phone = :phone, pass = :pass WHERE id = :id');
        $prepare->bindParam('id', $id, PDO::PARAM_INT);
        $prepare->bindParam('gid', $gid, PDO::PARAM_INT);
        $prepare->bindParam('pib', $pib, PDO::PARAM_STR);
        $prepare->bindParam('email', $email, PDO::PARAM_STR);
        $prepare->bindParam('phone', $phone, PDO::PARAM_STR);
        $prepare->bindParam('pass', $pass, PDO::PARAM_STR);
        $prepare->execute();
    }
    
    public function findById($id){
        $sql = "SELECT 
                    users.id, 
                    users.gid, 
                    groups.name AS group_name, 
                    users.pib, 
                    users.email, 
                    users.phone, 
                    users.pass 
                FROM 
                    users 
                    LEFT JOIN groups ON users.gid = groups.id 
                WHERE 
                    users.id = :id";
        $prepare = $this->db->prepare($sql);
        $prepare->bindParam('id', $id);
        $prepare->execute();
        $result = $prepare->fetchAll();
        if ( count($result) === 0 ){
            return null;
        }
        return $result[0];
    }
    
    public function del($id){
        $prepare = $this->db->prepare('DELETE FROM users WHERE id = :id');
        $prepare->bindParam('id', $id, PDO::PARAM_INT);
        $prepare->execute();
    }
    
}
